/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null) {
            return null;
        }
        Stack<ListNode> stack  = new Stack<>();
        while(head != null) {
            stack.push(head);
            head = head.next;
        }
        ListNode num = stack.pop();
        ListNode sum = num;
        int length = stack.size();
        for(int i = 0;i < length;i++) {
            sum.next = stack.pop();
            sum = sum.next;
        }
        sum.next = null;
        return num;
    }
}